package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseModel;
import com.isunland.managesystem.base.BaseObject;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.SimpleDetailParams;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/16 18:23
 */

public abstract class SimpleDetailFragment<T extends BaseModel> extends BaseFragment {
  protected static final int REQUEST_ALTER = 500;
  protected int mType;
  protected T mItem;
  private int mLayoutId;
  private int mTitleId;
  private Class<? extends BaseObject> mClassOriginal;

  @Override public void initData() {
    super.initData();
    SimpleDetailParams mParams = initParams();
    //noinspection unchecked
    mItem = (T) mBaseParams.getItem();
    mType = mBaseParams.getType();
    mLayoutId = mParams.getLayoutId();
    mTitleId = mParams.getTitleId();
    mClassOriginal = mParams.getClassOriginal();
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(mLayoutId, container, false);
    ButterKnife.bind(this, view);
    return view;
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    if (mType == BaseParams.TYPE_ADD) {
      setTitleCustom(MyStringUtil.connect(R.string.add, mTitleId));
    } else if (mType == BaseParams.TYPE_EDIT) {
      setTitleCustom(MyStringUtil.connect(R.string.alter, mTitleId));
    } else {
      setTitleCustom(MyStringUtil.connect(mTitleId, R.string.detail1));
    }
    if (mType == BaseParams.TYPE_ADD) {
      initViewAdd();
    }
    if (mType == BaseParams.TYPE_EDIT) {
      initViewEdit(mItem);
    }
    if (mType == BaseParams.TYPE_DETAIL) {
      initViewDetail(mItem);
    }
    bindData(mItem);
    volleyPostInit();
  }

  protected abstract void volleyPostInit();

  protected abstract void bindData(T mItem);

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;

    //修改完成,通知列表更新
    if (requestCode == REQUEST_ALTER) {
      mActivity.setResult(Activity.RESULT_OK);
      mActivity.finish();
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (mType == BaseParams.TYPE_ADD || mType == BaseParams.TYPE_EDIT) {
      inflater.inflate(R.menu.menu_confirm, menu);
    }
    if (mType == BaseParams.TYPE_DETAIL) {
      inflater.inflate(R.menu.menu_alter_delete, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_confirm:
        volleyPostConfirm();
        return true;
      case R.id.menu_item_alter:

        mBaseParams.setType(BaseParams.TYPE_EDIT);
        //noinspection unchecked
        mBaseParams.setItem(mItem);
        BaseVolleyActivity.newInstance(this, mActivity.getClass(), mBaseParams, REQUEST_ALTER);
        return true;
      case R.id.menu_item_delete:
        volleyPostDelete();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  protected abstract SimpleDetailParams initParams();

  protected abstract void initViewAdd();

  protected abstract void initViewEdit(T mItem);

  protected abstract void initViewDetail(T mItem);

  /** 确认,使用SimpleDetailResponse */
  protected abstract void volleyPostConfirm();
  /**
   * 在详情中禁用的view
   */
  //protected abstract void disableView();

  /**
   * 在详情和修改时的适配数据
   */
  //protected abstract void bindView(T mItem);

  /** 在添加或修改时的点击事件 */
  //protected abstract void setOnClick();

  /** 删除,使用SimpleDetailResponse */
  protected abstract void volleyPostDelete();

  protected class SimpleDetailResponse extends VolleyResponse {

    @Override public void onVolleyError(VolleyError arg0) {
      ToastUtil.show(R.string.failure_operation);
    }

    @Override public void onVolleyResponse(String json) {
      Base original = new Gson().fromJson(json, Base.class);
      ToastUtil.show(original.getMessage());
      if (original.getResult() == Base.SUCCESS) {
        mActivity.setResult(Activity.RESULT_OK);
        mActivity.finish();
      }
    }
  }

  protected class SimpleInitResponse extends VolleyResponse {

    @Override public void onVolleyError(VolleyError arg0) {

    }

    @Override public void onVolleyResponse(String json) {
      //noinspection unchecked
      T item = (T) (new Gson().fromJson(json, mClassOriginal).getData());
      if (item == null) return;
      mItem = item;
      bindData(mItem);
    }

    @Override public boolean ifToastError() {
      return false;
    }
  }
}
